// components/goodsFeature/goodsFeature.js
var ports = require("../../utils/ports.js")
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    goodsShopID: String,
    goodsShop: Object,
    featureItemList: Object,
    fitemid: String,
    fitemName: String,
    gshowPrice: Number,
    thisBotH:String,
    toggle: {
      type: Boolean,
      value: false,
      observer(v) {
        if (v) {
          this._open();
        } else {
          this._close();
        }
      }
    }
  },
  /**
   * 组件的初始数据
   */
  data: {
    show: false,
    animateData: {},
    amount: 1
  },
  attached() {    
  },
  ready() {
  },
  /**
   * 组件的方法列表
   */
  methods: {
    // 单品抵扣券点击确定选择红包
    confirm() {
      this.triggerEvent('confirm');
    },
    _open() {
      const animate = wx.createAnimation({
        duration: 300,
        timingFunction: "ease",
        delay: 0
      })
      animate.translate3d(0, 417, 0).step();
      this.setData({
        show: true,
        animateData: animate.export()
      })
      setTimeout(() => {
        animate.translate3d(0, 0, 0).step();
        this.setData({
          animateData: animate.export()
        })
      }, 50)
      this.getFeatureList(this.properties.goodsShopID)
    },
    _close() {
      const animate = wx.createAnimation({
        duration: 300,
        timingFunction: "ease",
        delay: 0
      })
      animate.translate3d(0, 417, 0).step();
      this.setData({
        animateData: animate.export()
      })
      setTimeout(() => {
        animate.translate3d(0, 417, 0).step();
        this.setData({
          animateData: animate.export(),
          show: false
        })
      }, 300)
      this.setData({
        amount: 1,
        fitemid: '',
        fitemName: ''
      })
    },

    close() {
      this.triggerEvent('close');
    },

    //减数量
    countdown() {
      const that = this;
      let amount = that.properties.amount;
      amount--;
      if (amount <= 0) {
        amount = 1;
      }
      that.setData({
        amount
      })
    },
    //加数量
    countadd() {
      const that = this;
      let amount = that.properties.amount;
      amount++;
      that.setData({
        amount
      })
    },
    //更改数量
    updateAmount(e) {
      const that = this;
      let amount = e.detail.value;
      that.setData({
        amount
      })
    },
    //获取商品规格
    getFeatureList(objectID) {
      var that = this
      //获取产品规格   
      ports.ModuleGoodsShop.getObjectFeatureItemList({
        objectID: objectID,
        pageNumber: 999
      }, that, res => {
        if (res.data.header.code == 0) {
          that.setData({
            featureItemList: res.data.body.data
          })
          if (that.data.featureItemList.length > 0) {
            that.setData({
              //默认选中第一个规格
              fitemName: that.data.featureItemList[0].itemList[0].objectFeatureItemName,
              fitemid: that.data.featureItemList[0].itemList[0].objectFeatureItemID
            })
          }
        }
      })
    },
    //点击规格
    featureItemChoose: function (e) {
      var that = this
      var thisid = e.currentTarget.dataset.id
      that.getObjectFeatureData(that.properties.goodsShopID, thisid)
      that.setData({
        fitemid: thisid,
        fitemName: e.currentTarget.dataset.fname
      })
    },
    //获取规格item
    getObjectFeatureData(objectID, objectFeatureItemID) {
      var that = this
      //获取产品规格
      ports.ModuleGoodsShop.getObjectFeatureData({
        objectID: objectID,
        objectFeatureItemID1: objectFeatureItemID
      }, that, res => {
        if (res.data.header.code == 0) {
          that.setData({
            gshowPrice: res.data.body.data.showPrice
          })
        }
      })
    },
    //加入购物车
    addcart: function () {
      var that = this
      var sessionID = wx.getStorageSync('USER_SESSIONID')
      if (sessionID) {
        wx.showToast({
          title: '提交中',
          icon: 'loading',
          mask: true
        })
        var data = {}
        data.goodsShopID = that.data.goodsShopID
        data.qty = that.data.amount
        data.objectFeatureItemID1 = that.data.fitemid
        console.log(data)
        //加入购物车
        ports.ModuleCart.addGoodsShopToCart(data, that, res => {
          console.log(res)
          wx.hideToast()
          if (res.data.header.code == 0) {
            that.setData({
              amount: 1
            })
            wx.showToast({
              title: "加入购物车成功",
              icon: 'none'
            })
            this.triggerEvent('close');
          } else {
            wx.showToast({
              title: res.data.header.msg,
              icon: 'none'
            })
          }
        })
      } else {
        wx.getUserProfile({
          desc: '用于完善会员资料',
          success: (res) => {
            if (res.userInfo != null) {
              wx.setStorageSync("USER_DEFAULT", {
                avatarUrl: res.userInfo.avatarUrl,
                nickName: res.userInfo.nickName
              })
              utils.doLoginOther()
            }
          }
        })
      }
    },
    //立即购买
    toConfirmOrder: function () {
      var that = this
      wx.navigateTo({
        url: '/pages/confirmOrder/confirmOrder?type=detail&qty=' + that.data.amount + "&id=" + that.data.goodsShopID + '&realPrice=' + that.data.gshowPrice + '&objectFeatureItemName1=' + that.data.fitemName + '&objectFeatureItemID1=' + that.data.fitemid,
      })
      that.triggerEvent('close');
    },
  }
})